package com.athena.web.controller.faculty;

import java.security.Principal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.athena.api.exam.IExamService;
import com.athena.api.faculty.IFacultyService;
import com.athena.dal.compositeEntities.FacultyRoutines;
import com.athena.dal.entities.Examination;
import com.athena.dal.entities.Faculty;
import com.athena.dal.entities.Subject;
import com.athena.dal.entities.User;
import com.athena.web.controller.service.AthenaUser;

@Controller
public class FacultyController {
	
	@Autowired
	private IFacultyService facultyService;
	
	@Autowired
	private IExamService examService;

	@RequestMapping(value="/faculty/home")
	public ModelAndView getFacultyHomePage(HttpServletRequest request, Principal principal){
		
		
		Faculty  faculty =  (Faculty)( (AthenaUser)( (UsernamePasswordAuthenticationToken)principal).getPrincipal()).getUser();
		request.setAttribute("page", "home");
		request.setAttribute("subpage", "home");
		
		ModelAndView andView = new ModelAndView("faculty/faculty-home", "faculty", faculty) ;
		return andView;
	}
	
	@RequestMapping(value="/calendar/teacher/day")
	@ResponseBody
	public List facultyCalendarDay(HttpServletRequest request, Principal principal){
		
		Faculty  faculty =  (Faculty)( (AthenaUser)( (UsernamePasswordAuthenticationToken)principal).getPrincipal()).getUser();
		request.setAttribute("page", "home");
		request.setAttribute("subpage", "home");
		Map<String, Object> map = new HashMap<String, Object>();
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		map.put("title", "pankaj");
		map.put("start", new Date());
		list.add(map);
		map = new HashMap<String, Object>();
		map.put("title", "vikas");
		map.put("start", new Date());
		list.add(map);
		return list;
	}
	
	
	@RequestMapping(value="/faculty/class")
	public ModelAndView getFacultyClass(HttpServletRequest request,Principal principal) throws ParseException{
		request.setAttribute("page", "class");
		request.setAttribute("subpage", "m");
		
		UsernamePasswordAuthenticationToken passwordAuthentication = (UsernamePasswordAuthenticationToken) principal;
		AthenaUser athenaUser= (AthenaUser) passwordAuthentication.getPrincipal();
		
		String sDate = request.getParameter("startDate");
		String eDate = request.getParameter("endDate");
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd");
		
		Date startDate = null;
		Date endDate = null;
		
	    if(sDate == null || eDate == null) {
	    	Calendar cal = Calendar.getInstance();
			cal.set(2013, 0, 1, 0, 0, 0);
	    	startDate = cal.getTime();
	    	endDate = cal.getTime();
	    } else {
	    	startDate = dateFormat.parse(sDate);
			endDate = dateFormat.parse(eDate);				    	
	    }
	    
		
		List<FacultyRoutines> routines = facultyService.getRoutine(athenaUser.getUser(), startDate.getTime(), endDate.getTime());
		
		ModelAndView andView = new ModelAndView("faculty/faculty-classes", "routines", routines) ;
		return andView;
	}
	
	@RequestMapping(value="/faculty/class/all")
	public ModelAndView getFacultyClassAll(HttpServletRequest request){
		request.setAttribute("page", "class");
		request.setAttribute("subpage", "a");
		Faculty faculty = new Faculty();
		faculty.setDegree("M A");
		faculty.setDesignation("HOD");
		faculty.setJoiningDate(new Date());
		faculty.setMajor("History");
		User user = new User();
		user.setUsername("manoj.gupta");
		/*user.setAddress("Lapta ganj");
		user.setCity("Delhi");
		user.setDistrict("Rohini");
		user.setEmail("gupta@gmail.com");
		user.setFirstName("Manoj");
		user.setLastName("Gupta");*/
		ModelAndView andView = new ModelAndView("faculty/faculty-home", "faculty", faculty) ;
		return andView;
	}
	
	@RequestMapping(value="/faculty/class/schedule")
	public ModelAndView getFacultyClassSchedule(HttpServletRequest request){
		request.setAttribute("page", "class");
		request.setAttribute("subpage", "sc");
		Faculty faculty = new Faculty();
		faculty.setDegree("M A");
		faculty.setDesignation("HOD");
		faculty.setJoiningDate(new Date());
		faculty.setMajor("History");
		User user = new User();
		user.setUsername("manoj.gupta");
		/*user.setAddress("Lapta ganj");
		user.setCity("Delhi");
		user.setDistrict("Rohini");
		user.setEmail("gupta@gmail.com");
		user.setFirstName("Manoj");
		user.setLastName("Gupta");*/
		ModelAndView andView = new ModelAndView("faculty/faculty-home", "faculty", faculty) ;
		return andView;
	}
	
	
	@RequestMapping(value="/faculty/class/routine")
	public ModelAndView getFacultyClassRoutine(HttpServletRequest request){
		request.setAttribute("page", "class");
		request.setAttribute("subpage", "cr");
		Faculty faculty = new Faculty();
		faculty.setDegree("M A");
		faculty.setDesignation("HOD");
		faculty.setJoiningDate(new Date());
		faculty.setMajor("History");
		User user = new User();
		user.setUsername("manoj.gupta");
		/*user.setAddress("Lapta ganj");
		user.setCity("Delhi");
		user.setDistrict("Rohini");
		user.setEmail("gupta@gmail.com");
		user.setFirstName("Manoj");
		user.setLastName("Gupta");*/
		ModelAndView andView = new ModelAndView("faculty/class/routine", "faculty", faculty) ;
		return andView;
	}
	
	
	@RequestMapping(value="/faculty/department")
	public ModelAndView getFacultyDepartment(HttpServletRequest request){
		request.setAttribute("page", "department");
		request.setAttribute("subpage", "m");
		Faculty faculty = new Faculty();
		faculty.setDegree("M A");
		faculty.setDesignation("HOD");
		faculty.setJoiningDate(new Date());
		faculty.setMajor("History");
		User user = new User();
		user.setUsername("manoj.gupta");
		/*user.setAddress("Lapta ganj");
		user.setCity("Delhi");
		user.setDistrict("Rohini");
		user.setEmail("gupta@gmail.com");
		user.setFirstName("Manoj");
		user.setLastName("Gupta");*/
		ModelAndView andView = new ModelAndView("faculty/faculty-home", "faculty", faculty) ;
		return andView;
	}
	
	@RequestMapping(value="/faculty/department/all")
	public ModelAndView getFacultyDepartmentAll(HttpServletRequest request){
		request.setAttribute("page", "department");
		request.setAttribute("subpage", "a");
		Faculty faculty = new Faculty();
		faculty.setDegree("M A");
		faculty.setDesignation("HOD");
		faculty.setJoiningDate(new Date());
		faculty.setMajor("History");
		User user = new User();
		user.setUsername("manoj.gupta");
		/*user.setAddress("Lapta ganj");
		user.setCity("Delhi");
		user.setDistrict("Rohini");
		user.setEmail("gupta@gmail.com");
		user.setFirstName("Manoj");
		user.setLastName("Gupta");*/
		ModelAndView andView = new ModelAndView("faculty/faculty-home", "faculty", faculty) ;
		return andView;
	}
	
	@RequestMapping(value="/faculty/attendance")
	public ModelAndView getFacultyAttendance(HttpServletRequest request){
		request.setAttribute("page", "attendance");
		request.setAttribute("subpage", "t");
		Faculty faculty = new Faculty();
		faculty.setDegree("M A");
		faculty.setDesignation("HOD");
		faculty.setJoiningDate(new Date());
		faculty.setMajor("History");
		User user = new User();
		user.setUsername("manoj.gupta");
		/*user.setAddress("Lapta ganj");
		user.setCity("Delhi");
		user.setDistrict("Rohini");
		user.setEmail("gupta@gmail.com");
		user.setFirstName("Manoj");
		user.setLastName("Gupta");*/
		ModelAndView andView = new ModelAndView("faculty/faculty-home", "faculty", faculty) ;
		return andView;
	}
	
	
	@RequestMapping(value="/faculty/attendance/past")
	public ModelAndView getFacultyAttendancePast(HttpServletRequest request){
		request.setAttribute("page", "attendance");
		request.setAttribute("subpage", "p");
		Faculty faculty = new Faculty();
		faculty.setDegree("M A");
		faculty.setDesignation("HOD");
		faculty.setJoiningDate(new Date());
		faculty.setMajor("History");
		User user = new User();
		user.setUsername("manoj.gupta");
		/*user.setAddress("Lapta ganj");
		user.setCity("Delhi");
		user.setDistrict("Rohini");
		user.setEmail("gupta@gmail.com");
		user.setFirstName("Manoj");
		user.setLastName("Gupta");*/
		ModelAndView andView = new ModelAndView("faculty/faculty-home", "faculty", faculty) ;
		return andView;
	}
	
	
	@RequestMapping(value="/faculty/attendance/record")
	public ModelAndView getFacultyAttendanceRecord(HttpServletRequest request){
		request.setAttribute("page", "attendance");
		request.setAttribute("subpage", "r");
		Faculty faculty = new Faculty();
		faculty.setDegree("M A");
		faculty.setDesignation("HOD");
		faculty.setJoiningDate(new Date());
		faculty.setMajor("History");
		User user = new User();
		user.setUsername("manoj.gupta");
		/*user.setAddress("Lapta ganj");
		user.setCity("Delhi");
		user.setDistrict("Rohini");
		user.setEmail("gupta@gmail.com");
		user.setFirstName("Manoj");
		user.setLastName("Gupta");*/
		ModelAndView andView = new ModelAndView("faculty/faculty-home", "faculty", faculty) ;
		return andView;
	}
	
	@RequestMapping(value="/faculty/exams/all")
	public ModelAndView getExamsForFaculty(HttpServletRequest request, Principal principal) {
		//System.out.println("Serving /faculty/exams/all");
		highlightExamInNav(request);
		
		Faculty currentFaculty = currentFaculty(principal);
		//Map<Integer, List<Examination>> allExams = null;
		List<Examination> allExams = null;
		if(currentFaculty != null) {
			allExams = examService.findForFaculty(currentFaculty.getId());
		}
		ModelAndView examView= new ModelAndView("faculty/exams/faculty-exams-all", "allExams", allExams) ;
		return examView;	
	}
	
	@RequestMapping(value="/faculty/exams/new")
	public ModelAndView createExam(HttpServletRequest request, Principal principal) {
		highlightExamInNav(request);
		Faculty currentFaculty = currentFaculty(principal);
		ModelAndView newExamView= new ModelAndView("faculty/exams/faculty-exams-new") ;
		if(currentFaculty != null) {
			List<Subject> listFacultySubjects = facultyService.teaches(currentFaculty.getId());
			newExamView.addObject("listFacultySubjects", listFacultySubjects);
		}
		return newExamView;
	}
	
	@RequestMapping(value="/faculty/exams/save")
	public void saveExam(HttpServletRequest request, Principal principal) {
		highlightExamInNav(request);
		System.out.println("############ Save exam called");
	}
	
	private void highlightExamInNav(HttpServletRequest request) {
		if(request != null) {
			request.setAttribute("page", "class");
			request.setAttribute("subpage", "e");
		}
	}
	
	private AthenaUser getAthenaUser(Principal principal) {
		if(principal != null) {
			Object user = ((UsernamePasswordAuthenticationToken)principal).getPrincipal();
			if(user != null && user instanceof AthenaUser) {
				return (AthenaUser) user;
			}
		}
		return null;
	}
	
	private Faculty currentFaculty(Principal principal){
		AthenaUser athenaUser = getAthenaUser(principal);
		if(athenaUser != null) {
			User user = athenaUser.getUser();
			if(user instanceof Faculty) {
				return (Faculty) user;
			}
		}
		
		return null;
	}


	/**
	 * @return the facultyService
	 */
	public IFacultyService getFacultyService() {
		return facultyService;
	}


	/**
	 * @param facultyService the facultyService to set
	 */
	public void setFacultyService(IFacultyService facultyService) {
		this.facultyService = facultyService;
	}


	/**
	 * @return the examService
	 */
	public IExamService getExamService() {
		return examService;
	}


	/**
	 * @param examService the examService to set
	 */
	public void setExamService(IExamService examService) {
		this.examService = examService;
	}
	
}
